/**
 * elancom Corporation copy right 2012 - 2015  
 *   
 * $Header: https://192.168.1.224/svn/elancom/product/elancom-platform/trunk/src/main/java/com/elancom/tools/AddSVNKeywords.java 1034 2012-07-02 03:09:12Z lanzhouxue $
 * $Id: AddSVNKeywords.java 1034 2012-07-02 03:09:12Z lanzhouxue $
 * $Author: lanzhouxue $
 * $Date: 2012-07-02 11:09:12 +0800 (Mon, 02 Jul 2012) $
 * $Revision: 1034 $
 */
package com.elancom.framework.commons.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URL;

/**
 * @author elancom
 * @date 2012-6-15
 */
public class AddSVNKeywords {
	private static String PACKAGE_MARK = "package com.elancom";
	private static String REPLAEC_PATH = "/src/main/java";
	private static String BUILD_PATH = "/target/classes/";
	private static String KW = "";

	static {
		final URL url = Thread.currentThread().getContextClassLoader().getResource(".");
		String path = url.getPath();
		path = path.substring(1);
		path = path.replace(BUILD_PATH, "");
		REPLAEC_PATH = path + REPLAEC_PATH;

		final StringBuffer sb = new StringBuffer();
		sb.append("/**").append("\n");
		sb.append(" * elancom Corporation copy right 2012 - 2015  ").append("\n");
		sb.append(" *   ").append("\n");
		sb.append(
				" * $Header: https://192.168.1.224/svn/elancom/product/elancom-platform/trunk/src/main/java/com/elancom/tools/AddSVNKeywords.java 1034 2012-07-02 03:09:12Z lanzhouxue $")
				.append("\n");
		sb.append(" * $Id: AddSVNKeywords.java 1034 2012-07-02 03:09:12Z lanzhouxue $").append("\n");
		sb.append(" * $Author: lanzhouxue $").append("\n");
		sb.append(" * $Date: 2012-07-02 11:09:12 +0800 (Mon, 02 Jul 2012) $").append("\n");
		sb.append(" * $Revision: 1034 $").append("\n");
		sb.append(" */").append("\n");
		KW = sb.toString();
	}

	public static void writeKW(File f) throws IOException {
		if (f.isDirectory()) {
			for (final File file : f.listFiles(new java.io.FileFilter() {
				@Override
				public boolean accept(File pathname) {
					final String fileName = pathname.getName();
					if (pathname.isHidden()) {
						return false;
					} else if (pathname.isDirectory()) {
						if (fileName.equals(".svn")) {
							return false;
						}
					}
					return pathname.isFile() ? fileName.lastIndexOf(".java") == fileName.length() - 5 : true;
				}
			})) {
				writeKW(file);
			}
		} else {
			writeFile(f);
		}
	}

	private static void writeFile(File file) throws IOException {
		final BufferedReader bufferedReader = new BufferedReader(new FileReader(file), 1024 * 1024 * 10); // 10M
		String line = bufferedReader.readLine();

		final BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));
		int i = 0;
		while (line != null) {
			if (i++ < 3 && line.startsWith(PACKAGE_MARK)) {
				bufferedWriter.append(KW);
			}

			bufferedWriter.append(line);
			bufferedWriter.newLine();
			line = bufferedReader.readLine();
		}

		bufferedReader.close();
		bufferedWriter.close();
		System.out.println("成功添加->" + file.getName());
	}

	public static void main(String[] args) throws IOException {
		if (PACKAGE_MARK.equals("false$#LKJ!@#LKJ:LKJC:LKDSJ:LKJF:SLDKU")) {
			// File file = new File(REPLAEC_PATH);
			// writeKW(file);
		}
	}
}
